$(function (){
    $.ajaxSetup({
        beforeSend: function(xhr) {
            //console.log(xhr);
            xhr.setRequestHeader('token', localStorage.getItem('HTTP_TOKEN'));
        },
        data:{
            //'token': localStorage.getItem('HTTP_TOKEN')
        },
        error: function (res){
            if(res.status === 401){
                location.href='login.html';
            }
            notice(res.code + res.msg, 'error');
        }
    });

var recordlist = [];
    // 获取领取详情
    $.ajax({
        'type': 'get',
        'url': domain + '/api/pb/getRecord',
        success: function (res){
            if(res.code === 1){
                let data = res.data;
                // 订购记录
                $('.loginwrap .order-item .price').html(data.order.money + '元');
                $('.loginwrap .order-item .type span').html('支付成功');
                $('.loginwrap .order-item .time').html(data.order.create_time);
                $('.loginwrap .login .phone').html(data.order.phone);

                // 领取记录
                for(let i = 0; i < data.record.length; i++){
                    // data.record[i].idkey = i;
                    let record = data.record[i];
                    recordlist.push(record) ;
// console.log(data);
// console.log(record);
                    let quan = '<div data-v-e286eb19="" class="infoitem'+ (record.status === 0 || record.is_receive === 1 ? ' greyout' : '')  +(i > 1 ? ' thirdafter' : '')      +'">' +
                        '                            <img data-v-e286eb19="" src="image/10y2.png">\n' +
                        '                            <div data-v-e286eb19="" style="font-size: 0.5rem;">10元话费</div>\n' +
                        '                        </div>';
                    $('.equitieslist .memberlist').append(quan);

                    if(record.receive){
                        let status = '';
                        switch (record.receive.status){
                            case 0:
                                status = '待发放';
                                break;
                            case 1:
                                status = '已发放';
                                break;
                            case 2:
                                status = '人工流程中，请耐心等待';
                                break;
                            case 3:
                                status = '人工流程中，请耐心等待';
                                break;
                        }
                        let receive = '<div data-v-e286eb19="" class="infoitem">\n' +
                            '                                    <div data-v-e286eb19="" class="name">十元话费</div>\n' +
                            '                                    <div data-v-e286eb19="" class="type"><span data-v-e286eb19="">'+ status +'</span></div>\n' +
                            '                                    <div data-v-e286eb19="" class="time">'+ record.receive.create_time +'</div>\n' +
                            '                                </div>';
                        $('.record .list').append(receive);
                    }
                }
            }else{
                notice(res.msg, 'error');
            }
        }
    });

var thirdtime = '';

    // 领取
    $(document).on('click', '.equitieslist .infoitem', function () {
        let _this = $(this);
        if(_this.hasClass('greyout')){    // 已经领取会提示这里
            // notice('已领取', 'warning');
            notice('您已领取过该权益，请勿重复领取', 'warning');
            return false;
        }
        
        for(let i = 0; i < recordlist.length; i++){
            if(recordlist[i].is_receive == 0){
                 thirdtime = recordlist[i].end_time;
                break;
            }
        }

        // 恭喜您已经领取成功，请等待话费充值到账
        // 本月从未点击过 和点击领取后的提示不一样
        
        // if(_this.hasClass('thirdafter')){
        //     if(new Date(thirdtime).getTime() < new Date().getTime()){         // 通知后台用户已申请领取
        //         notice('恭喜您已经领取成功，请等待话费充值到账', 'warning');
        //     }else{
        //         // notice('本月会员权益您已领取过，请在'+thirdtime+'后再来领取', 'warning');
        //         notice('本月会员权益您已领取过，请在'+thirdtime+'后再来领取', 'warning');     //未到领取时间提示这个
        //         // 已经点击领取的话，提示 恭喜您已经领取成功，请等待话费充值到账
        //     }
        //     return false;
        // }

        $.ajax({
            type: 'get',
            url: domain + '/api/pb/receive',
            data:{thirdafter:_this.hasClass('thirdafter')},
            success: function (res) {
                if(res.code == 1){
                    $(".equitieslist .infolist div[class=infoitem]:eq(0)").addClass('greyout');
                    notice(res.msg, 'success');
                }else{
                    notice(res.msg, 'error');
                }
            }
        });

    });

    //关闭
    $(".loginwrap .close").on('click', function () {
        $(".loginmask").hide().find('.item').hide();
    });

    // 领取记录
    $(".handlebtnwrap .record").on('click', function () {
        $(".loginmask").show().find('.record').show();
    });

    // 订购记录
    $(".handlebtnwrap .order").on('click', function () {
        $(".loginmask").show().find('.order').show();
    });

    // 退出登录
    $(".handlebtnwrap .login").on('click', function () {
        $(".loginmask").show().find('.login').show();
    });

    $(".loginwrap .sublogin").on('click', function () {
        localStorage.removeItem('HTTP_TOKEN');
        location.href = 'login.html';
    });
});

function notice(msg, error='succcess'){
    let notice = '<div data-v-5e62a61f="" class="animate__animated '+ error +' animate__fadeOutUp" style="top: 2rem;">\n' +
        '                '+ msg +'.\n' +
        '            </div>';


    $('.messagePop').html(notice).slideDown();

    setTimeout(function () {
        $('.messagePop').html('').slideUp();
    }, 2000);
}